package com.ebt.m.data.rxModel;

import com.ebt.m.AppContext;
import com.ebt.m.data.middle.Security;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Andy.Wang on 2016/3/9.
 */
public class RxDataRequest {

    public static final String EBT_USER = "zyjandroid001";
    public static final String EBT_KEY = "1opbgwsjcqnitjo2c6bpdk9c8zyskyk0";
    public static final String GETPRODUCTJSON = "/dat/product/json/param1";
    public static final String GET_ATTATCH_PRODUCT_LIST = "/dat/products/additional/param1/param2";
    public static final String GET_ORDER_STR = "/dat/brand/productorder/param1";
    public static final String POST_TO_FEEDBACK = "/api/common/feedback";
    public static final String FEEDBACK_TYPES = "/api/common/syscode";
    public static final String GET_BRAND_TAGS_STR = "/dat/product/tags/brand/param1";
    public static final String GET_PRODUCTS_TAGS_STR = "/dat/product/tags/product/param1";

    public static String getRestHeader(String type, Object... param) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("ebtUser", EBT_USER);

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String ebtTime = simpleDateFormat.format(AppContext.getCurrentTime());
            jsonObject.put("ebtTime", ebtTime);
            String url = "";
            if (param == null || param.length == 0) {
                url = type;
            } else {
                url = type;
                for (int i = 0; i < param.length; i++) {
                    String paramStr = "param" + (i + 1);
                    url = url.replaceAll(paramStr, (String) param[i]);
                }
            }
            Map<String, String> map = new HashMap<String, String>();
            map.put("url", url);
            map.put("ebtUser", EBT_USER);
            map.put("ebtTime", ebtTime);
            String text = createLinkString(paraFilter(map)) + EBT_KEY;
            String ebtSign = Security.MD5Encrypt(text, "");
            jsonObject.put("ebtSign", ebtSign);
            return jsonObject.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static Map<String, String> paraFilter(Map<String, String> sArray) {

        Map<String, String> result = new HashMap<String, String>();
        if (sArray == null || sArray.size() <= 0) {
            return result;
        }
        for (String key : sArray.keySet()) {
            String value = sArray.get(key);
            if (value == null || value.equals("") || key.equalsIgnoreCase("ebtSign")) {
                continue;
            }
            result.put(key, value);
        }
        return result;
    }


    private static String createLinkString(Map<String, String> params) {
        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);
        String prestr = "";

        for (int i = 0, t = keys.size(); i < t; i++) {
            String key = keys.get(i);
            String value = params.get(key);
            if (value != null && !value.equals("")) {
                if (i == keys.size() - 1) {
                    prestr = prestr + key + "=" + value;
                } else {
                    prestr = prestr + key + "=" + value + "&";
                }
            }
        }
        return prestr;
    }


}
